<article id="wikiArticle">
<div></div>
<h2 id="消息">消息</h2>
<pre><code class="language-javascript">TypeError: invalid 'instanceof' operand "x" (Firefox) 
TypeError: "x" is not a function (Firefox) 
TypeError: Right-hand side of 'instanceof' is not an object (Chrome) 
TypeError: Right-hand side of 'instanceof' is not callable (Chrome)</code></pre>
<h2 id="错误类型">错误类型</h2>
<p><a href="Reference/Global_Objects/TypeError" title="TypeError（类型错误） 对象用来表示值的类型非预期类型时发生的错误。"><code>TypeError</code></a></p>
<h2 id="哪里出错了？">哪里出错了？</h2>
<p><a href="/en-US/docs/Web/JavaScript/Reference/Operators/instanceof"><code>instanceof</code> 操作符</a> 希望右边的操作数为一个构造对象，即一个有 <code>prototype</code> 属性且可以调用的对象。</p>
<h2 id="例子">例子</h2>
<pre><code class="language-js example-bad">"test" instanceof ""; // TypeError: invalid 'instanceof' operand ""
42 instanceof 0;      // TypeError: invalid 'instanceof' operand 0

function Foo() {}
var f = Foo();        // Foo() is called and returns undefined
var x = new Foo();

x instanceof f;       // TypeError: invalid 'instanceof' operand f
x instanceof x;       // TypeError: x is not a function
</code></pre>
<p>为了解决上述问题， 你可能需要将<a href="/en-US/docs/Web/JavaScript/Reference/Operators/instanceof"><code>instanceof</code> 操作符</a> 换成 <a href="/en-US/docs/Web/JavaScript/Reference/Operators/typeof"><code>typeof</code> 操作符</a>， 或者确保你使用的是函数名称，而不是函数计算的结果。</p>
<pre><code class="language-js example-good">typeof "test" == "string"; // true
typeof 42 == "number"      // true

function Foo() {}
var f = Foo;               // Do not call Foo.
var x = new Foo();

x instanceof f;            // true
x instanceof Foo;          // true
</code></pre>
<h2 id="参见">参见</h2>
<ul>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/instanceof"><code>instanceof</code> 操作符</a></li>
<li><a href="/en-US/docs/Web/JavaScript/Reference/Operators/typeof"><code>typeof</code> 操作符</a></li>
</ul>
<p> </p>
</article>